﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;

public partial class ProductManage_Manage_ProductType_AgingInfoManage : System.Web.UI.Page
{
    ProductType_AgingInfo productType;
    ProductType_AgingInfoInfo productTypeInfo;
    public string seachFactor
    {
        get { return ViewState["SeachFactor"].ToString(); }
        set { ViewState["SeachFactor"] = value; }
    }

    public ProductManage_Manage_ProductType_AgingInfoManage()
    {
        productType = new ProductType_AgingInfo();
        productTypeInfo = new ProductType_AgingInfoInfo();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            seachFactor = string.Empty;
            BindProductType();
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        seachFactor = this.TextBox1.Text.Trim();
        BindProductType();
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        seachFactor = string.Empty;
        BindProductType();
    }

    public void BindProductType()
    {
        if (string.IsNullOrEmpty(seachFactor))
        {
            GridView1.DataSource = productType.GetPAIs();
            GridView1.DataBind();
        }
        else
        {
            GridView1.DataSource = productType.GetPAIsbyPID(seachFactor);
            GridView1.DataBind();
        }
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex;
        BindProductType();
    }

    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        BindProductType();
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                ((LinkButton)e.Row.Cells[8].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除!')");
            }
        }
        if (((DropDownList)e.Row.FindControl("DDLAttributes")) != null)
        {   
            DropDownList ddlattributes = (DropDownList)e.Row.FindControl("DDLAttributes");
            ddlattributes.SelectedValue = ((HiddenField)e.Row.FindControl("HDFAttributes")).Value.Trim();
        }
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string productTypeID = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
        string agingStep = GridView1.DataKeys[e.RowIndex].Values[4].ToString();
        productType.Delete(productTypeID,agingStep);
        BindProductType();
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindProductType();
    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //更新时ProductTypeID和AgingStep是不能更改的，只能删除重新添加
        try
        {
            //(GridView1.DataKeys[e.RowIndex].Values[0].ToString(), GridView1.DataKeys[e.RowIndex].Values[1].ToString(), GridView1.DataKeys[e.RowIndex].Values[2].ToString(), GridView1.DataKeys[e.RowIndex].Values[3].ToString());
            productTypeInfo.ProductTypeID = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
            productTypeInfo.ProductTypeName = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.Trim().ToString();
            productTypeInfo.TimeSpec = decimal.Parse(((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.Trim());
            productTypeInfo.TempSpec = int.Parse(((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.Trim().ToString());
            productTypeInfo.AgingStep = GridView1.DataKeys[e.RowIndex].Values[4].ToString();
            productTypeInfo.Attributes = ((DropDownList)(GridView1.Rows[e.RowIndex].FindControl("DDLAttributes"))).SelectedValue;
            productTypeInfo.Department = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Text.Trim().ToString();
        }
        catch(FormatException)
        {
            string str1 = "录入数据格式有问题，请检查！";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
        }
        productType.Update(productTypeInfo);
        GridView1.EditIndex = -1;
        BindProductType();
    }

    //protected void Button1_Click(object sender, EventArgs e)
    //{        
    //    try
    //    {
    //        productTypeInfo = new ProductType_AgingInfoInfo(TextBox1.Text.Trim(), TextBox2.Text.Trim(), decimal.Parse(TextBox3.Text.Trim())
    //            , int.Parse(TextBox4.Text.Trim()), this.DropDownList1.SelectedValue, this.DropDownList2.SelectedValue, TextBox7.Text.Trim());
    //    }
    //    catch
    //    {
    //        string str1 = "录入数据格式有问题，请检查！";
    //        ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
    //    }
    //    ProductType_AgingInfoInfo pro = productType.GetPAI(TextBox1.Text.Trim(), this.DropDownList1.SelectedValue);
    //    if (pro.ProductTypeID == productTypeInfo.ProductTypeID)
    //    {
    //        string str1 = "此编号和时效阶段的产品已存在！";
    //        ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
    //    }
    //    else
    //    {
    //        productType.Insert(productTypeInfo);
    //        string str1 = "添加成功！";
    //        ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
    //        BindProductType();
    //    }
    //}

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Insert")
        {
            GridView1.DataSource = null;
            GridView1.DataBind();
        }
    }

    protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
        try
        {
            DetailsView d = (DetailsView)this.GridView1.Controls[0].Controls[0].FindControl("DetailsView1");
            productTypeInfo = new ProductType_AgingInfoInfo();
            productTypeInfo.ProductTypeID = ((TextBox)d.Rows[0].Cells[1].Controls[0]).Text.Trim();
            productTypeInfo.ProductTypeName = ((TextBox)d.Rows[1].Cells[1].Controls[0]).Text.Trim();
            productTypeInfo.TimeSpec = decimal.Parse(((TextBox)d.Rows[2].Cells[1].Controls[0]).Text.Trim());
            productTypeInfo.TempSpec = int.Parse(((TextBox)d.Rows[3].Cells[1].Controls[0]).Text.Trim());
            productTypeInfo.AgingStep = ((DropDownList)(d.Rows[4].Cells[1].FindControl("DDLAgingStep"))).SelectedValue;
            productTypeInfo.Attributes = ((DropDownList)(d.Rows[5].Cells[1].FindControl("DDLAttributes"))).SelectedValue;
            productTypeInfo.Department = ((TextBox)(d.Rows[6].Cells[1].Controls[0])).Text.Trim();
        }
        catch(FormatException)
        {
            string str1 = "录入数据格式有问题，请检查！";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
        }
        ProductType_AgingInfoInfo pro = productType.GetPAI(productTypeInfo.ProductTypeID, productTypeInfo.AgingStep);
        if (pro.ProductTypeID == productTypeInfo.ProductTypeID)
        {
            string str1 = "此编号和时效阶段的产品已存在！";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
        }
        else
        {
            productType.Insert(productTypeInfo);
            string str1 = "添加成功！";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
            BindProductType();
        }
    }

    protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
    {
        BindProductType();
    }
}
